package com.lebang.controller.sys.cences;

import com.lebang.common.Login;
import com.lebang.configuration.datasource.DataSource;
import com.lebang.domain.CencesRouseClick;
import com.lebang.example.CencesRouseClickExample;
import com.lebang.model.MyConfig;
import com.lebang.service.cences.CencesRouseClickService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;


/**
 * cences_rouse_click
 *
 * @author xwj
 * @date 2020-10-09
 */
@Controller
@RequestMapping("/sys/cences/cencesrouseclick")
public class CencesRouseClickController {
    @Autowired
    private CencesRouseClickService baseService;

    @Login
    @GetMapping("/list")
    public String list(Long rouseId, Model model) {
        model.addAttribute("rouseId",rouseId);
        return "/cencesrouseclick/list";
    }

    /**
     * @param page  页数
     * @param limit 条数
     * @param star  开始日期
     * @param end   结束日期
     * @return 返回数据
     */
    @PostMapping("/page")
    @ResponseBody
    @DataSource("commonDB")
    @Login
    public HashMap newList(Long page, Integer limit, Integer star, Integer end,Long rouseId) {
        HashMap map = new HashMap(1);
        CencesRouseClickExample example = new CencesRouseClickExample();
        CencesRouseClickExample.Criteria c = example.createCriteria();
        example.setOffset((page - 1) * limit);
        example.setLimit(limit);
        example.setOrderByClause("time desc,id desc");
        c.andAppIdEqualTo(MyConfig.getEnv());
        if (star != null && end != null) {
            c.andTimeBetween(star, end);
        }
        if(!TextUtil.isEmpty(rouseId)){
            c.andRouseIdEqualTo(rouseId);
        }
        map.put("count", this.baseService.countByExample(example));
        map.put("code", 0);

        List<CencesRouseClick> masters = baseService.selectByExample(example);
        map.put("data", masters);
        return map;
    }


    @Login
    @PostMapping("/delete")
    @ResponseBody
    @DataSource("commonDB")
    public JsonData delete(Integer id) {
        baseService.deleteByTime(baseService.selectById(id).getTime());
        return Callback.success("success", "删除成功");
    }


//    /**
//     * 统计指定日期渠道活跃人数
//     *
//     * @param date 日期
//     * @return 返回结果
//     */
//    @Login
//    @PostMapping("/lastCheck")
//    @ResponseBody
//    public String cen(String date) {
//        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
//        LocalDate end = LocalDate.parse(date, fmt);
//        baseService.starDataInit(LocalDateTime.of(end.plusDays(1), LocalTime.MIN));
//        return "success";
//    }
}
